<?php
class Archives extends Controller
{
	public function _default()
	{
		$this->lists();
	}
	
	public function lists()
	{
		$cid = isset($_REQUEST['cid']) ? intval($_REQUEST['cid']) : '';
		$page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
		$pageSize = isset($_REQUEST['pageSize']) ? intval($_REQUEST['pageSize']) : 30;
		
		$where = '';
		if ($cid){
			$subCatalog = CatalogModel::getList($cid);
			$cids[] = $cid;
			if ($subCatalog){
				foreach ($subCatalog as $item){
					$cids[] = $item['id'];
				}
			}
			$where = "cid in(".implode(',', $cids).")";
		}
		$catalogList = CatalogModel::getList(0);
		$archivesList = ArchivesModel::getList($where, $page, $pageSize);
		$count = ArchivesModel::getCount($where);
		$pageNav = Util::page($count, $pageSize);
		$this->assign('cid', $cid);
		$this->assign('pageNav', $pageNav);
		$this->assign('pageSize', $pageSize);
		$this->assign('catalogList', $catalogList);
		$this->assign('archivesList', $archivesList);
		$this->display('archives.tpl');
	}
	
	public function add()
	{
		if (isset($_POST['act'])){
			$act = trim($_POST['act']);
			$cid = isset($_REQUEST['cid']) ? intval($_REQUEST['cid']) : Util::msgRedirect('请选择栏目');
			$title = isset($_REQUEST['title']) && $_REQUEST['title'] ? trim($_REQUEST['title']) : Util::msgRedirect('缺少标题');
			$pic = isset($_REQUEST['pic']) ? trim($_REQUEST['pic']) : '';
			$flag = isset($_REQUEST['flag']) ? $_REQUEST['flag'] : '';
			$author = isset($_REQUEST['author']) ? trim($_REQUEST['author']) : '';
			$memo = isset($_REQUEST['memo']) && $_REQUEST['memo'] ? $_REQUEST['memo'] : Util::msgRedirect('缺少正文');
			$checkFirstPic = isset($_REQUEST['checkFirstPic']) ? intval($_REQUEST['checkFirstPic']) : 0;
			$priority = isset($_REQUEST['priority']) ? intval($_REQUEST['priority']) : 0;
			$priority = $priority ? time()+$priority*86400 : 0;
			$memo = stripslashes($memo);
			if (!$pic && $checkFirstPic){
				$result = array();
				preg_match_all('/\/Upload[\w+\/\.-]+\.[jpg|JPG|gif|GIF|png|PNG]+/', $memo, $result);
				$pic = isset($result[0][0]) ? $result[0][0] : '';
			}
			if ($pic){
				$pic = str_replace(APP_HOST, '', $pic);
			}
			if ($act=='add'){
				$aid = ArchivesModel::add($cid, $title, $memo, $pic, $flag, $author, $priority);
				if ($aid){
					Util::msgRedirect('添加成功', '/Admin/Archives');
				}else{
					Util::msgRedirect('添加失败');
				}
			}else{
				$aid = isset($_REQUEST['aid']) && $_REQUEST['aid'] ? intval($_REQUEST['aid']) : Util::msgRedirect('缺少文章ID');
				$data = array(
					'title'		=> $title,
					'cid'		=> $cid,
					'pic'		=> $pic,
					'priority'	=> $priority,
					'author'	=> $author,
					'flag'		=> $flag,
					'memo'		=> $memo
				);
				if (ArchivesModel::update($aid, $data)){
					Util::msgRedirect('更新成功', '/Admin/Archives');
				}else{
					Util::msgRedirect('更新失败');
				}
			}
			
		}else{
			$this->assign('catalogList', CatalogModel::getList(0));
			$this->assign('flags', ArchivesModel::$flag);
			$this->display('archives_add.tpl');
		}
	}
	
	public function edit()
	{
		$aid = isset($_REQUEST['aid']) && $_REQUEST['aid'] ? intval($_REQUEST['aid']) : Util::msgRedirect('缺少文章ID');
		$archives = ArchivesModel::get($aid);
		if (!$archives){
			Util::msgRedirect('文章信息不存在');
		}
		$this->assign('arr', $archives);
		$this->assign('act', 'edit');
		$this->add();
	}
}